<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Java学习笔记-JDBC(MySQL) | ShenShilei's Blog</title><meta name="keywords" content="mysql"><meta name="author" content="Shen Shilei"><meta name="copyright" content="Shen Shilei"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><meta name="description" content="学习MySQL数据库，以及通过JDBC操作MySQL数据库.">
<meta property="og:type" content="article">
<meta property="og:title" content="Java学习笔记-JDBC(MySQL)">
<meta property="og:url" content="https://blog.shenshilei.site/post/5740/index.html">
<meta property="og:site_name" content="ShenShilei&#39;s Blog">
<meta property="og:description" content="学习MySQL数据库，以及通过JDBC操作MySQL数据库.">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/132.JPG">
<meta property="article:published_time" content="2020-05-26T11:00:01.000Z">
<meta property="article:modified_time" content="2020-07-14T07:37:18.000Z">
<meta property="article:author" content="Shen Shilei">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="MySQL">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/132.JPG"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://blog.shenshilei.site/post/5740/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: {"limitDay":180,"position":"top","messagePrev":"这是一篇创建于","messageNext":"天前的文章，其中的信息可能已经有所发展或是发生改变。"},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: Shen Shilei","link":"链接: ","source":"来源: ShenShilei's Blog","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  ClickShowText: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"top-center"},
  justifiedGallery: {
    js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
    css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
};

var saveToLocal = {
  set: function setWithExpiry(key, value, ttl) {
    const now = new Date()
    const expiryDay = ttl * 86400000
    const item = {
      value: value,
      expiry: now.getTime() + expiryDay,
    }
    localStorage.setItem(key, JSON.stringify(item))
  },

  get: function getWithExpiry(key) {
    const itemStr = localStorage.getItem(key)

    if (!itemStr) {
      return undefined
    }
    const item = JSON.parse(itemStr)
    const now = new Date()

    if (now.getTime() > item.expiry) {
      localStorage.removeItem(key)
      return undefined
    }
    return item.value
  }
}</script><script id="config_change">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-07-14 15:37:18'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(function () {  window.activateDarkMode = function () {
    document.documentElement.setAttribute('data-theme', 'dark')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
    }
  }
  window.activateLightMode = function () {
    document.documentElement.setAttribute('data-theme', 'light')
   if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
    }
  }
  const autoChangeMode = '1'
  const t = saveToLocal.get('theme')
  if (autoChangeMode === '1') {
    const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
    const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
    const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
    const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
    if (t === undefined) {
      if (isLightMode) activateLightMode()
      else if (isDarkMode) activateDarkMode()
      else if (isNotSpecified || hasNoSupport) {
        const now = new Date()
        const hour = now.getHours()
        const isNight = hour <= 6 || hour >= 18
        isNight ? activateDarkMode() : activateLightMode()
      }
      window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
        if (saveToLocal.get('theme') === undefined) {
          e.matches ? activateDarkMode() : activateLightMode()
        }
      })
    } else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else if (autoChangeMode === '2') {
    const now = new Date()
    const hour = now.getHours()
    const isNight = hour <= 6 || hour >= 18
    if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
    else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else {
    if (t === 'dark') activateDarkMode()
    else if (t === 'light') activateLightMode()
  }})()</script><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Noto+Serif+SC"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/kang-static/Hexo/css/iconfont.min.css"><style type="text/css">.aplayer.aplayer-narrow .aplayer-body{left: -66px !important;transition: left 1s;} .aplayer.aplayer-narrow .aplayer-body:hover{left: 0 !important;}</style><style type="text/css">body{cursor: url(https://cdn.jsdelivr.net/gh/Justlovesmile/CDN/image/cursor.ico),default}</style><meta name="generator" content="Hexo 5.4.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="https://cdn.jsdelivr.net/gh/shenshilei1022/cdn@1.6/img/custom/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">92</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">60</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">38</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/artitalk/"><i class="fa-fw fa fa-comment"></i><span> 说说</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 文章</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></li><li><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page" href="/analytics/"><i class="fa-fw fas fa-chart-line"></i><span> 统计</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/bangumis/"><i class="fa-fw fab fa-youtube"></i><span> 番剧</span></a></li><li><a class="site-page" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li><li><a class="site-page" href="/books/"><i class="fa-fw fas fa-book"></i><span> 书单</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 工具箱</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/box/"><i class="fa-fw fas fa-th-large"></i><span> 网站聚合</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://cdn.jsdelivr.net/gh/shenshilei1022/cdn@2.0/"><i class="fa-fw fa fa-hdd"></i><span> MyCDN</span></a></li><li><a class="site-page" href="/note/"><i class="fa-fw fas fa-business-time"></i><span> 便利贴</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://music.shenshilei.site/"><i class="fa-fw fas fa-music"></i><span> 音乐播放器</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://webssh.shenshilei.site/"><i class="fa-fw fas fa-terminal"></i><span> SSH在线连接</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/comment/"><i class="fa-fw fa fa-pencil-alt"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-paperclip"></i><span> 关于</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/about/"><i class="fa-fw fas fa-rocket"></i><span> 我</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://shenshilei1022.avosapps.us/"><i class="fa-fw fas fa-list-alt"></i><span> 评论管理</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="http://bt.shenshilei.site:8888/shenshilei"><i class="fa-fw fas fa-window-maximize"></i><span> 服务器管理</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://console.leancloud.app/applist.html#/apps"><i class="fa-fw fas fa-window-restore"></i><span> leancloud应用</span></a></li></ul></div></div></div></div><div id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/132.JPG)"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">ShenShilei's Blog</a></span><span id="menus"><div id="search_button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/artitalk/"><i class="fa-fw fa fa-comment"></i><span> 说说</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 文章</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></li><li><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page" href="/analytics/"><i class="fa-fw fas fa-chart-line"></i><span> 统计</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 清单</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/bangumis/"><i class="fa-fw fab fa-youtube"></i><span> 番剧</span></a></li><li><a class="site-page" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li><li><a class="site-page" href="/books/"><i class="fa-fw fas fa-book"></i><span> 书单</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 工具箱</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/box/"><i class="fa-fw fas fa-th-large"></i><span> 网站聚合</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://cdn.jsdelivr.net/gh/shenshilei1022/cdn@2.0/"><i class="fa-fw fa fa-hdd"></i><span> MyCDN</span></a></li><li><a class="site-page" href="/note/"><i class="fa-fw fas fa-business-time"></i><span> 便利贴</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://music.shenshilei.site/"><i class="fa-fw fas fa-music"></i><span> 音乐播放器</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://webssh.shenshilei.site/"><i class="fa-fw fas fa-terminal"></i><span> SSH在线连接</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/comment/"><i class="fa-fw fa fa-pencil-alt"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-paperclip"></i><span> 关于</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page" href="/about/"><i class="fa-fw fas fa-rocket"></i><span> 我</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://shenshilei1022.avosapps.us/"><i class="fa-fw fas fa-list-alt"></i><span> 评论管理</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="http://bt.shenshilei.site:8888/shenshilei"><i class="fa-fw fas fa-window-maximize"></i><span> 服务器管理</span></a></li><li><a class="site-page" target="_blank" rel="noopener" href="https://console.leancloud.app/applist.html#/apps"><i class="fa-fw fas fa-window-restore"></i><span> leancloud应用</span></a></li></ul></div></div><span class="close" id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></span></span></nav><div id="post-info"><h1 class="post-title">Java学习笔记-JDBC(MySQL)</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-05-26T11:00:01.000Z" title="发表于 2020-05-26 19:00:01">2020-05-26</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2020-07-14T07:37:18.000Z" title="更新于 2020-07-14 15:37:18">2020-07-14</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Java/">Java</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/Java/%E6%95%B0%E6%8D%AE%E5%BA%93%E6%93%8D%E4%BD%9C/">数据库操作</a></span></div><div class="meta-secondline"> <span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.5k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>15分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span><span class="post-meta-separator">|</span><span class="post-meta-commentcount"><i class="far fa-comments fa-fw post-meta-icon"></i><span class="post-meta-label">评论数:</span><a href="/post/5740/#post-comment" itemprop="discussionUrl"><span class="valine-comment-count comment-count" data-xid="/post/5740/" itemprop="commentCount"></span></a></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="1-MySQL-关系型数据库"><a href="#1-MySQL-关系型数据库" class="headerlink" title="1. MySQL(关系型数据库)"></a>1. MySQL(关系型数据库)</h2><p>Databases 简称：DB</p>
<h3 id="1-1-什么是数据库？"><a href="#1-1-什么是数据库？" class="headerlink" title="1.1 什么是数据库？"></a>1.1 什么是数据库？</h3><ul>
<li>用于存储和管理数据的仓库</li>
</ul>
<h3 id="1-2-数据库的特点："><a href="#1-2-数据库的特点：" class="headerlink" title="1.2 数据库的特点："></a>1.2 数据库的特点：</h3><ul>
<li>持久化存储数据。其实数据库就是一个文件系统。</li>
<li>方便存储和管理数据。</li>
<li>使用了统一的方式操作数据库。 —SQL</li>
</ul>
<h3 id="1-3-安装过程"><a href="#1-3-安装过程" class="headerlink" title="1.3 安装过程"></a>1.3 安装过程</h3><ol>
<li>官网下载zip文件（Downloads-MySQL Community Edition-MySQL Community Server-ZIP Archive）</li>
<li>直接解压到对应的文件夹。</li>
<li>新建my.ini配置文件（在bin同级目录下）。</li>
</ol>
<figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="section">[mysql]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置mysql客户端默认字符集</span></span><br><span class="line"></span><br><span class="line"><span class="attr">default-character-set</span>=utf8 </span><br><span class="line"></span><br><span class="line"><span class="section">[mysqld]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#设置3306端口</span></span><br><span class="line"></span><br><span class="line"><span class="attr">port</span> = <span class="number">3306</span> </span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置mysql的安装目录</span></span><br><span class="line"></span><br><span class="line"><span class="attr">basedir</span>=D:\Software\MySQL\mysql-<span class="number">8.0</span>.<span class="number">17</span>-winx64</span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置mysql数据库的数据的存放目录</span></span><br><span class="line"></span><br><span class="line"><span class="attr">datadir</span>=D:\Software\MySQL\mysql-<span class="number">8.0</span>.<span class="number">17</span>-winx64\data</span><br><span class="line"></span><br><span class="line"><span class="comment"># 允许最大连接数</span></span><br><span class="line"></span><br><span class="line"><span class="attr">max_connections</span>=<span class="number">200</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 服务端使用的字符集默认为8比特编码的latin1字符集</span></span><br><span class="line"></span><br><span class="line"><span class="attr">character-set-server</span>=utf8</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建新表时将使用的默认存储引擎</span></span><br><span class="line"></span><br><span class="line"><span class="attr">default-storage-engine</span>=INNODB</span><br></pre></td></tr></table></figure>
<ol>
<li>使用管理员权限打开cmd窗口，切换到对应的bin目录下，使用命令。</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">mysqld --install</span><br><span class="line">mysqld --initialize</span><br><span class="line">net start mysql</span><br></pre></td></tr></table></figure>
<ol>
<li>在MySQL安装目录下搜索.err文件，其中localhost后面的字符对应初始密码。</li>
<li><code>alter user user() identified by &quot;123456&quot;;</code>更改密码。</li>
</ol>
<p><img src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/131.JPG" alt=""></p>
<p>安装的MySQL实际上是一个服务，在计算机管理中的服务中来启动和停止MySQL的服务。cmd —&gt; service.msc</p>
<p>net start mysql</p>
<p>net stop mysql</p>
<h3 id="1-4-MySQL的登录和退出"><a href="#1-4-MySQL的登录和退出" class="headerlink" title="1.4 MySQL的登录和退出"></a>1.4 MySQL的登录和退出</h3><p>登录：</p>
<ol>
<li><code>mysql -u 用户名 -p</code></li>
<li><code>mysql -h ip地址 -u 用户名 -p</code></li>
</ol>
<p>退出：</p>
<ol>
<li><code>exit</code></li>
<li><code>quit</code></li>
</ol>
<h2 id="2-SQL"><a href="#2-SQL" class="headerlink" title="2. SQL"></a>2. SQL</h2><h3 id="2-1-什么是SQL？"><a href="#2-1-什么是SQL？" class="headerlink" title="2.1 什么是SQL？"></a>2.1 什么是SQL？</h3><p>Structured Query Language:结构化查询语言.其实就是定义了操作所有关系型数据库的规则。</p>
<h3 id="2-2-SQL通用语法"><a href="#2-2-SQL通用语法" class="headerlink" title="2.2 SQL通用语法"></a>2.2 SQL通用语法</h3><ol>
<li>SQL语句可以单行或多行书写，以分号结尾。</li>
<li>可使用空格和缩进来增强语句的可读性。</li>
<li>MySQL数据库的SQL语句不区分大小写，但关键字建议用大写。</li>
<li>注释<ul>
<li>单行注释：— 注释内容</li>
<li>多行注释：/<em> 注释 </em>/</li>
</ul>
</li>
</ol>
<h2 id="3-使用SQL操作数据库和表"><a href="#3-使用SQL操作数据库和表" class="headerlink" title="3. 使用SQL操作数据库和表"></a>3. 使用SQL操作数据库和表</h2><h3 id="3-1-操作数据库"><a href="#3-1-操作数据库" class="headerlink" title="3.1 操作数据库"></a>3.1 操作数据库</h3><p>一般都有增查改删(CRUD, Create, Retrieve, Update, Delete)四个操作.</p>
<p><em>展示已有数据库</em></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SHOW</span> DATABASES;</span><br></pre></td></tr></table></figure>
<p><em>查询当前正在使用的数据库</em></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> DATABASE();</span><br></pre></td></tr></table></figure>
<p><em>新建数据库java</em></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> DATABASE java;</span><br></pre></td></tr></table></figure>
<p><em>使用新建的java数据库</em></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">USE java;</span><br></pre></td></tr></table></figure>
<p><em>删除数据库java</em></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DROP</span> DATABASE java;</span><br></pre></td></tr></table></figure>
<p><strong>这样删除有一个缺点，如果不存在java数据库时执行这条命令会报错，可以使用下面的方法。</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DROP</span> DATABASE IF <span class="keyword">EXISTS</span> java;</span><br></pre></td></tr></table></figure>
<h3 id="3-2-操作表"><a href="#3-2-操作表" class="headerlink" title="3.2 操作表"></a>3.2 操作表</h3><p>同样地，也是进行增查改删。</p>
<p>MySQL数据类型：</p>
<ol>
<li>int：整数类型<br> age int;</li>
<li>double：小数类型<br> score double(5, 2) — 5表示数字最长有5位，2表示保留2位小数</li>
<li>date：日期，只包括年月日，yyyy-MM-dd</li>
<li>datetime：日期，包括年月日时分秒，yyyy-MM-dd HH:mm:ss</li>
<li>timestamp：时间戳类型，包含年月日时分秒，    yyyy-MM-dd HH:mm:ss<br> 如果将来不给这个字段赋值，或复制为null，则默认使用当前系统时间<br> <em>注意，添加时间戳类型，需要把默认值设置为current_timestamp(当前时间戳)才能正确获取到当前系统时间。</em><pre><code> ALTER TABLE stu ADD update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
</code></pre></li>
<li>varchar：字符串<br> name varchar(20); — 姓名最大20个字符<br> zhangsan 8个字符； 张三 2个字符</li>
</ol>
<p><em>新建表personInfo</em></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> personInfo (</span><br><span class="line">    id <span class="type">INT</span> auto_increment,</span><br><span class="line">    name <span class="type">VARCHAR</span>(<span class="number">20</span>),</span><br><span class="line">    age <span class="type">INT</span>,</span><br><span class="line">    <span class="keyword">PRIMARY</span> KEY (id)</span><br><span class="line">) <span class="keyword">DEFAULT</span> CHARSET<span class="operator">=</span>utf8;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>PRIMARY KEY为主键约束，为了便于更快的查找表中的数据，都会在表中设置一个主键。<br>注意：(设置的时候记得带上括号👀)</p>
<ul>
<li>每个表只能定义一个主键。</li>
<li>不能为NULL。</li>
</ul>
</blockquote>
<p><em>新增数据</em></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> personInfo <span class="keyword">VALUES</span> (<span class="keyword">NULL</span>, <span class="string">&#x27;小明&#x27;</span>, <span class="number">23</span>);</span><br></pre></td></tr></table></figure>
<p><strong>因为id设置了自增，所以可以插入null</strong></p>
<p><em>查询数据</em></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> personInfo;</span><br></pre></td></tr></table></figure>
<p>也可以根据条件查询</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> personInfo <span class="keyword">WHERE</span> id<span class="operator">=</span><span class="number">1</span>;</span><br></pre></td></tr></table></figure>
<p><em>分页查询</em></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> personInfo LIMIT <span class="number">0</span>, <span class="number">5</span>;</span><br></pre></td></tr></table></figure>
<p><strong>这条命令表示从第1条数据开始，共查五条。</strong></p>
<p><em>修改数据</em></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">UPDATE personInfo <span class="keyword">SET</span> age<span class="operator">=</span><span class="number">25</span> <span class="keyword">WHERE</span> name<span class="operator">=</span><span class="string">&#x27;小明&#x27;</span>;</span><br></pre></td></tr></table></figure>
<p><em>删除数据</em></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DELETE</span> <span class="keyword">FROM</span> personInfo <span class="keyword">WHERE</span> id<span class="operator">=</span><span class="number">1</span>;</span><br></pre></td></tr></table></figure>
<p><strong>没有条件时则删除表中全部数据！</strong></p>
<p>删除多条数据可以使用类似分页查询的limit，但后面只跟一个参数count，即删除多少条数据</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DELETE</span> <span class="keyword">FROM</span> personInfo LIMIT <span class="number">5</span>;</span><br></pre></td></tr></table></figure>
<p>其它的一些知识(例如外键约束，事务等等)等到用到了再进行整理与学习🧑</p>
<h2 id="4-JDBC"><a href="#4-JDBC" class="headerlink" title="4. JDBC"></a>4. JDBC</h2><p>JDBC(Java DataBase Connection)通过Java访问数据库。</p>
<h3 id="4-1-使用Maven新建项目"><a href="#4-1-使用Maven新建项目" class="headerlink" title="4.1 使用Maven新建项目"></a>4.1 使用Maven新建项目</h3><p>选择quickstart模板新建项目。</p>
<p>在pom.xml配置文件中添加依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>8.0.17<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>这里我踩了两个坑！首先是添加的依赖版本和我使用的MySQL版本不同，建立连接报错为java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long，然后我把版本修改为一致，结果建立连接继续报错😥，报错信息为The server time zone value ‘�й���׼ʱ��’ is unrecognized or represents more than one time zone.提示为时区问题，需要通过连接设置serverTimezone来解决。使用格林威治时间设置为serverTimezone=GMT;北京时间则为serverTimezone=GMT%2B8。</p>
</blockquote>
<h3 id="4-2-Hello-JDBC"><a href="#4-2-Hello-JDBC" class="headerlink" title="4.2 Hello JDBC"></a>4.2 Hello JDBC</h3><h4 id="4-2-1-初始化驱动并建立连接"><a href="#4-2-1-初始化驱动并建立连接" class="headerlink" title="4.2.1 初始化驱动并建立连接"></a>4.2.1 初始化驱动并建立连接</h4><p>需要提供：</p>
<ul>
<li>数据库所处的ip：127.0.0.1或localhost(本机)</li>
<li>数据库工作的端口号：3306(MySQL专用端口号)</li>
<li>数据库名称：personInfo</li>
<li>编码方式：UTF-8</li>
<li>账号：root</li>
<li>密码：admin</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.sql.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestJDBC</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Class.forName(<span class="string">&quot;com.mysql.cj.jdbc.Driver&quot;</span>);</span><br><span class="line">            System.out.println(<span class="string">&quot;初始化驱动成功！&quot;</span>);</span><br><span class="line">            Connection c = DriverManager.getConnection(<span class="string">&quot;jdbc:mysql://127.0.0.1:3306/personInfo?characterEncoding=UTF-8&amp;serverTimezone=GMT&quot;</span>, <span class="string">&quot;root&quot;</span>, <span class="string">&quot;admin&quot;</span>);</span><br><span class="line">            System.out.println(<span class="string">&quot;连接成功，获取连接对象：&quot;</span> + c);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ClassNotFoundException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>如果看到控制台输出下面内容，则建立连接成功。</p>
<p><img src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/130.jpg" alt=""></p>
<blockquote>
<p>Class.forName和new类似，都是创建一个实例，但Class.forName的好处是：如果成功完成，那么这个类已经加载且已经连接。</p>
</blockquote>
<h4 id="4-2-2-新建Statement运行SQL语句"><a href="#4-2-2-新建Statement运行SQL语句" class="headerlink" title="4.2.2 新建Statement运行SQL语句"></a>4.2.2 新建Statement运行SQL语句</h4><p>例如在表中新增一条数据</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.sql.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestJDBC</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Class.forName(<span class="string">&quot;com.mysql.cj.jdbc.Driver&quot;</span>);</span><br><span class="line">            System.out.println(<span class="string">&quot;初始化驱动成功！&quot;</span>);</span><br><span class="line">            Connection c = DriverManager.getConnection(<span class="string">&quot;jdbc:mysql://127.0.0.1:3306/personInfo?characterEncoding=UTF-8&amp;serverTimezone=GMT&quot;</span>, <span class="string">&quot;root&quot;</span>, <span class="string">&quot;admin&quot;</span>);</span><br><span class="line">            System.out.println(<span class="string">&quot;连接成功，获取连接对象：&quot;</span> + c);</span><br><span class="line">            Statement s = c.createStatement();</span><br><span class="line">            String sql = String.format(<span class="string">&quot;INSERT INTO personInfo VALUES (NULL, %s, %d)&quot;</span>, <span class="string">&quot;&#x27;小刚&#x27;&quot;</span>, <span class="number">18</span>);</span><br><span class="line">            s.execute(sql);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ClassNotFoundException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>注意：name对应的字段需要先加单引号，再加上双引号作为java字符串😜</strong></p>
<p>增改删均使用Statement实例的execute方法；</p>
<p>而查询则使用executeQuery方法，将返回一个ResultSet，通过next方法移动光标，遍历内容。</p>
<h4 id="4-2-3-关闭连接"><a href="#4-2-3-关闭连接" class="headerlink" title="4.2.3 关闭连接"></a>4.2.3 关闭连接</h4><p>用完之后记得养成关闭的好习惯，否则造成对资源的占用。</p>
<p>先关Statement，再关Connection。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.sql.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestJDBC</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Class.forName(<span class="string">&quot;com.mysql.cj.jdbc.Driver&quot;</span>);</span><br><span class="line">            System.out.println(<span class="string">&quot;初始化驱动成功！&quot;</span>);</span><br><span class="line">            Connection c = DriverManager.getConnection(<span class="string">&quot;jdbc:mysql://127.0.0.1:3306/personInfo?characterEncoding=UTF-8&amp;serverTimezone=GMT&quot;</span>, <span class="string">&quot;root&quot;</span>, <span class="string">&quot;admin&quot;</span>);</span><br><span class="line">            System.out.println(<span class="string">&quot;连接成功，获取连接对象：&quot;</span> + c);</span><br><span class="line">            Statement s = c.createStatement();</span><br><span class="line">            String sql = String.format(<span class="string">&quot;INSERT INTO personInfo VALUES (NULL, %s, %d)&quot;</span>, <span class="string">&quot;&#x27;小刚&#x27;&quot;</span>, <span class="number">18</span>);</span><br><span class="line">            s.execute(sql);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ClassNotFoundException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (s != <span class="keyword">null</span>) &#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    s.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (SQLException) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (c != <span class="keyword">null</span>) &#123;</span><br><span class="line">                <span class="keyword">try</span> &#123;</span><br><span class="line">                    c.close();</span><br><span class="line">                &#125; <span class="keyword">catch</span> (SQLException) &#123;</span><br><span class="line">                    e.printStackTrace();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>如果觉得这样关闭比较繁琐，可以参考关闭流的方式，使用try-with-resource的方式自动关闭连接，因为Connection和Statement都实现了AutoCloseable接口。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.sql.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestJDBC</span> </span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Class.forName(<span class="string">&quot;com.mysql.cj.jdbc.Driver&quot;</span>);</span><br><span class="line">            System.out.println(<span class="string">&quot;初始化驱动成功！&quot;</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ClassNotFoundException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">try</span> (Connection c = DriverManager.getConnection(<span class="string">&quot;jdbc:mysql://127.0.0.1:3306/personInfo?characterEncoding=UTF-8&amp;serverTimezone=GMT&quot;</span>, <span class="string">&quot;root&quot;</span>, <span class="string">&quot;admin&quot;</span>);</span><br><span class="line">            System.out.println(<span class="string">&quot;连接成功，获取连接对象：&quot;</span> + c);</span><br><span class="line">            Statement s = c.createStatement()) &#123;</span><br><span class="line">            String sql = String.format(<span class="string">&quot;INSERT INTO personInfo VALUES (NULL, %s, %d)&quot;</span>, <span class="string">&quot;&#x27;小刚&#x27;&quot;</span>, <span class="number">18</span>);</span><br><span class="line">            s.execute(sql);</span><br><span class="line">        &#125;  <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="5-高阶操作"><a href="#5-高阶操作" class="headerlink" title="5. 高阶操作"></a>5. 高阶操作</h2><h3 id="5-1-使用PreparedStatement"><a href="#5-1-使用PreparedStatement" class="headerlink" title="5.1 使用PreparedStatement"></a>5.1 使用PreparedStatement</h3><p>PreparedStatement继承了Statement，也是用来执行sql语句的，但是它有几个好处：</p>
<ol>
<li>参数设置：<br>Statement需要进行字符串拼接，容易出错；<br>PreparedStatement可以使用？占位符，后期进行设置，易维护。</li>
<li>性能表现：<br>插入多条数据时，PreparedStatement比Statement更快。</li>
<li>防止SQL注入式攻击<br>例如name是用户提交的数据”name=’小明’ OR 1=1”<br>此时使用Statement进行字符串拼接，得到的SQL语句是”SELECT * FROM personInfo WHERE name=’小明’ OR 1=1”，由于1=1是恒成立的条件，所以这样就会把所有数据都查出来，而不仅仅名字为小明的数据。如果表中的数据是海量的，那么将会把内存全部消耗光，造成极大的不便；而使用PreparedStatement，使用？对name进行占位，那么即使用户输入了”name=’小明’ OR 1=1”，预编译也会将它作为一整个字段放在name处，结果就是什么都查不到，而不会出现内存占满的情况。</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">String sql = <span class="string">&quot;SELECT * FROM personInfo WHERE name=?&quot;</span>;</span><br><span class="line"><span class="comment">// c是Connection，这里没写对应的代码</span></span><br><span class="line">PreparedStatement ps = c.prepareStatement(sql);</span><br><span class="line">ps.setString(<span class="number">1</span>, <span class="string">&quot;小明&quot;</span>);</span><br><span class="line">ps.execute();</span><br></pre></td></tr></table></figure>
<p><strong>注意：ResultSet和PreparedStatement是这里唯二索引从1开始数的，不要弄错啦🤓</strong></p>
<blockquote>
<p>个人理解如果要执行的sql语句不用设置参数就直接使用Statement，需要就用PreparedStatement🤗</p>
</blockquote>
<h3 id="5-2-事务"><a href="#5-2-事务" class="headerlink" title="5.2 事务"></a>5.2 事务</h3><p>事务相当于打包多个操作，这多个操作要么都成功，要么都失败，不会出现只执行其中一个操作的情况。例如业务操作需要将一个人的年龄加一再减一，不用事务的话，减一sql语句写错了，结果就是年龄将会加一；而使用事务，其中一个语句出错，那么整个事务都不会执行，年龄信息将不变。</p>
<p>通过<code>c.setAutoCommit(false);</code>关闭自动提交</p>
<p>使用<code>c.commit();</code>进行手动提交</p>
<p>此时，增改删语句execute后没有真正生效，需要提交后才会生效。</p>
<blockquote>
<p>这里MySQL需要使用innodb存储引擎，在my.ini配置文件中设置<code>default-storage-engine=INNODB</code>，在MySQL中执行下面的语句<code>SELECT SUPPORT FROM INFORMATION_SCHEMA.ENGINES WHERE ENGINE=&#39;InnoDB&#39;;</code>，如果innodb被启用并且是默认数据库，那么会出现SUPPORT-DEFAULT，如果innodb可用，但不是默认引擎，则结果是yes；否则是no。(我的MySQL版本是8.0.17，使用此方法可行，删除data文件夹下的ib文件不可用。)</p>
</blockquote>
<h3 id="5-3-ORM和DAO"><a href="#5-3-ORM和DAO" class="headerlink" title="5.3 ORM和DAO"></a>5.3 ORM和DAO</h3><p>ORM(Object Relationship Database Mapping)，对象和关系数据库的映射。即一个对象对应数据库的一条记录。</p>
<p>通过将对象看作一条数据，使用java对对象和记录相互操作。</p>
<p>DAO(Data Access Object)，数据访问对象。和ORM思路相同，把数据库相关的操作都封装在这个类中，其它地方看不到JDBC的代码。</p>
<p><em>新建PersonInfo类</em></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PersonInfo</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> id;</span><br><span class="line">    <span class="keyword">public</span> String name;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">int</span> age;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><em>新建DAO接口(写成泛型格式)</em></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">DAO</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 增添数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(T t)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 查询单个数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> T <span class="title">get</span><span class="params">(<span class="keyword">int</span> id)</span></span>;</span><br><span class="line">    <span class="comment">// 查询全部数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;T&gt; <span class="title">list</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="comment">// 分页查询</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;T&gt; <span class="title">list</span><span class="params">(<span class="keyword">int</span> start, <span class="keyword">int</span> count)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 修改数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">update</span><span class="params">(T t)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 删除数据</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">delete</span><span class="params">(<span class="keyword">int</span> id)</span></span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><em>新建PersonInfoDAO类</em></p>
<p>初始化驱动放在构造方法中，使用getConnection方法获取与数据库的连接。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.ssl;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.*;</span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PersonInfoDAO</span> <span class="keyword">implements</span> <span class="title">DAO</span>&lt;<span class="title">PersonInfo</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 初始化驱动放在构造方法中</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">PersonInfoDAO</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Class.forName(<span class="string">&quot;com.mysql.cj.jdbc.Driver&quot;</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ClassNotFoundException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 建立连接</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Connection <span class="title">getConnection</span><span class="params">()</span> <span class="keyword">throws</span> SQLException </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> DriverManager.getConnection(<span class="string">&quot;jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8&amp;serverTimezone=GMT&quot;</span>, <span class="string">&quot;root&quot;</span>, <span class="string">&quot;admin&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">(PersonInfo personInfo)</span> </span>&#123;</span><br><span class="line">        String sql = <span class="string">&quot;INSERT INTO personInfo VALUES (NULL, ?, ?)&quot;</span>;</span><br><span class="line">        <span class="keyword">try</span> (Connection c = getConnection();</span><br><span class="line">             PreparedStatement ps = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) &#123;</span><br><span class="line">            ps.setString(<span class="number">1</span>, personInfo.name);</span><br><span class="line">            ps.setInt(<span class="number">2</span>, personInfo.age);</span><br><span class="line">            ps.execute();</span><br><span class="line"></span><br><span class="line">            <span class="comment">// 添加完后将自增的id再填回personInfo中</span></span><br><span class="line">            ResultSet rs = ps.getGeneratedKeys();</span><br><span class="line">            <span class="keyword">if</span> (rs.next()) &#123;</span><br><span class="line">                personInfo.id = rs.getInt(<span class="number">1</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;插入数据成功！&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> PersonInfo <span class="title">get</span><span class="params">(<span class="keyword">int</span> id)</span> </span>&#123;</span><br><span class="line">        PersonInfo person = <span class="keyword">null</span>;</span><br><span class="line">        String sql = <span class="string">&quot;SELECT * FROM personInfo WHERE id=?&quot;</span>;</span><br><span class="line">        <span class="keyword">try</span> (Connection c = getConnection();</span><br><span class="line">        PreparedStatement ps = c.prepareStatement(sql)) &#123;</span><br><span class="line">            ps.setInt(<span class="number">1</span>, id);</span><br><span class="line">            ResultSet rs = ps.executeQuery();</span><br><span class="line">            <span class="keyword">while</span> (rs.next()) &#123;</span><br><span class="line">                String name = rs.getString(<span class="string">&quot;name&quot;</span>);</span><br><span class="line">                <span class="keyword">int</span> age = rs.getInt(<span class="string">&quot;age&quot;</span>);</span><br><span class="line">                person = <span class="keyword">new</span> PersonInfo();</span><br><span class="line">                person.id = id;</span><br><span class="line">                person.name = name;</span><br><span class="line">                person.age = age;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> person;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;PersonInfo&gt; <span class="title">list</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> list(<span class="number">0</span>, Short.MAX_VALUE);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;PersonInfo&gt; <span class="title">list</span><span class="params">(<span class="keyword">int</span> start, <span class="keyword">int</span> count)</span> </span>&#123;</span><br><span class="line">        List&lt;PersonInfo&gt; personList = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        String sql = <span class="string">&quot;SELECT * FROM personInfo LIMIT ?, ?&quot;</span>;</span><br><span class="line">        <span class="keyword">try</span> (Connection c = getConnection();</span><br><span class="line">             PreparedStatement ps = c.prepareStatement(sql)) &#123;</span><br><span class="line">            ps.setInt(<span class="number">1</span>, start);</span><br><span class="line">            ps.setInt(<span class="number">2</span>, count);</span><br><span class="line">            ResultSet rs = ps.executeQuery();</span><br><span class="line">            <span class="keyword">while</span> (rs.next()) &#123;</span><br><span class="line">                <span class="keyword">int</span> id = rs.getInt(<span class="string">&quot;id&quot;</span>);</span><br><span class="line">                String name = rs.getString(<span class="string">&quot;name&quot;</span>);</span><br><span class="line">                <span class="keyword">int</span> age = rs.getInt(<span class="string">&quot;age&quot;</span>);</span><br><span class="line">                PersonInfo person = <span class="keyword">new</span> PersonInfo();</span><br><span class="line">                person.id = id;</span><br><span class="line">                person.name = name;</span><br><span class="line">                person.age = age;</span><br><span class="line">                personList.add(person);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> personList;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getTotal</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        String sql = <span class="string">&quot;SELECT COUNT(*) FROM personInfo&quot;</span>;</span><br><span class="line">        <span class="keyword">int</span> total = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">try</span> (Connection c = getConnection();</span><br><span class="line">             Statement s = c.createStatement()) &#123;</span><br><span class="line">            ResultSet rs = s.executeQuery(sql);</span><br><span class="line">            <span class="keyword">while</span> (rs.next()) &#123;</span><br><span class="line">                total = rs.getInt(<span class="number">1</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> total;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">update</span><span class="params">(PersonInfo personInfo)</span> </span>&#123;</span><br><span class="line">        String sql = <span class="string">&quot;UPDATE personInfo SET name=?, age=? WHERE id=?&quot;</span>;</span><br><span class="line">        <span class="keyword">try</span> (Connection c = getConnection();</span><br><span class="line">             PreparedStatement ps = c.prepareStatement(sql)) &#123;</span><br><span class="line">            ps.setString(<span class="number">1</span>, personInfo.name);</span><br><span class="line">            ps.setInt(<span class="number">2</span>, personInfo.age);</span><br><span class="line">            ps.setInt(<span class="number">3</span>, personInfo.id);</span><br><span class="line">            ps.execute();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;修改成功！&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">delete</span><span class="params">(<span class="keyword">int</span> id)</span> </span>&#123;</span><br><span class="line">        String sql = <span class="string">&quot;DELETE FROM personInfo WHERE id=?&quot;</span>;</span><br><span class="line">        <span class="keyword">try</span> (Connection c = getConnection();</span><br><span class="line">             PreparedStatement ps = c.prepareStatement(sql)) &#123;</span><br><span class="line">            ps.setInt(<span class="number">1</span>, id);</span><br><span class="line">            ps.execute();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;删除成功！&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="6-参考"><a href="#6-参考" class="headerlink" title="6. 参考"></a>6. 参考</h2><ul>
<li><a target="_blank" rel="noopener" href="https://how2j.cn/k/mysql/mysql-install/377.html#nowhere">MySQL</a></li>
<li><a target="_blank" rel="noopener" href="https://how2j.cn/k/jdbc/jdbc-mysql/386.html">JDBC</a></li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Shen Shilei</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://blog.shenshilei.site/post/5740/">https://blog.shenshilei.site/post/5740/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://blog.shenshilei.site" target="_blank">ShenShilei's Blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Java/">Java</a><a class="post-meta__tags" href="/tags/MySQL/">MySQL</a></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/132.JPG" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏<div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/WeChanSQ.png" target="_blank"><img class="post-qr-code-img" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/WeChanSQ.png" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/AliPayQR.png" target="_blank"><img class="post-qr-code-img" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/AliPayQR.png" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/post/e95d/"><img class="prev-cover" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/158.JPG" onerror="onerror=null;src='/img/404.jpg'"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">MySQL学习-连接的使用</div></div></a></div><div class="next-post pull-right"><a href="/post/c54e/"><img class="next-cover" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/120.jpg" onerror="onerror=null;src='/img/404.jpg'"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">《侠探简不知》观后感</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/post/1750/" title="将Web项目部署在阿里云服务器"><img class="cover" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/96.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-05-08</div><div class="title">将Web项目部署在阿里云服务器</div></div></a></div><div><a href="/post/b1ef/" title="Java学习笔记-JSP"><img class="cover" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/FormentorHolidays_ZH-CN3392936755_1920x1080.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-04</div><div class="title">Java学习笔记-JSP</div></div></a></div><div><a href="/post/ca10/" title="Java学习笔记-Tomcat和Servlet"><img class="cover" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/PascuaFlorida_ZH-CN7720904158_1920x1080.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-03-31</div><div class="title">Java学习笔记-Tomcat和Servlet</div></div></a></div><div><a href="/post/9ee3/" title="Java学习笔记-数字与字符串"><img class="cover" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/Windhorses_ZH-CN5349922758_1920x1080.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-02-29</div><div class="title">Java学习笔记-数字与字符串</div></div></a></div><div><a href="/post/3da9/" title="使用QQ音乐Api批量下载音乐"><img class="cover" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/115.JPG" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-05-22</div><div class="title">使用QQ音乐Api批量下载音乐</div></div></a></div><div><a href="/post/44e3/" title="设计购物车小项目"><img class="cover" src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/171.JPG" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-06-03</div><div class="title">设计购物车小项目</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div class="vcomment" id="vcomment"></div></div></div></div></div><div class="aside_content" id="aside_content"><div class="card-widget card-info"><div class="card-content"><div class="card-info-avatar is-center"><img class="avatar-img" src="https://cdn.jsdelivr.net/gh/shenshilei1022/cdn@1.6/img/custom/avatar.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">Shen Shilei</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">92</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">60</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">38</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/shenshilei1022"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/shenshilei1022" target="_blank" title="Github"><i class="iconfont icon-github1"></i></a><a class="social-icon" href="mailto:1774822536@qq.com" target="_blank" title="Email"><i class="iconfont icon-youxiang"></i></a><a class="social-icon" href="https://gitee.com/shenshilei1022" target="_blank" title="Gitee"><i class="iconfont icon-github2"></i></a><a class="social-icon" href="https://music.163.com/#/user/home?id=77600006" target="_blank" title="Netease Music"><i class="iconfont icon-wangyiyunyinle"></i></a><a class="social-icon" href="https://space.bilibili.com/15250718" target="_blank" title="Bilibili"><i class="iconfont icon-bilibili1"></i></a></div></div></div><div class="card-widget card-announcement"><div class="card-content"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">活在当下</div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="card-content"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-MySQL-%E5%85%B3%E7%B3%BB%E5%9E%8B%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-text">1. MySQL(关系型数据库)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-%E4%BB%80%E4%B9%88%E6%98%AF%E6%95%B0%E6%8D%AE%E5%BA%93%EF%BC%9F"><span class="toc-text">1.1 什么是数据库？</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E7%89%B9%E7%82%B9%EF%BC%9A"><span class="toc-text">1.2 数据库的特点：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-%E5%AE%89%E8%A3%85%E8%BF%87%E7%A8%8B"><span class="toc-text">1.3 安装过程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-4-MySQL%E7%9A%84%E7%99%BB%E5%BD%95%E5%92%8C%E9%80%80%E5%87%BA"><span class="toc-text">1.4 MySQL的登录和退出</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-SQL"><span class="toc-text">2. SQL</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-%E4%BB%80%E4%B9%88%E6%98%AFSQL%EF%BC%9F"><span class="toc-text">2.1 什么是SQL？</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-SQL%E9%80%9A%E7%94%A8%E8%AF%AD%E6%B3%95"><span class="toc-text">2.2 SQL通用语法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E4%BD%BF%E7%94%A8SQL%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E5%BA%93%E5%92%8C%E8%A1%A8"><span class="toc-text">3. 使用SQL操作数据库和表</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-text">3.1 操作数据库</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-%E6%93%8D%E4%BD%9C%E8%A1%A8"><span class="toc-text">3.2 操作表</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-JDBC"><span class="toc-text">4. JDBC</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-%E4%BD%BF%E7%94%A8Maven%E6%96%B0%E5%BB%BA%E9%A1%B9%E7%9B%AE"><span class="toc-text">4.1 使用Maven新建项目</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-Hello-JDBC"><span class="toc-text">4.2 Hello JDBC</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-1-%E5%88%9D%E5%A7%8B%E5%8C%96%E9%A9%B1%E5%8A%A8%E5%B9%B6%E5%BB%BA%E7%AB%8B%E8%BF%9E%E6%8E%A5"><span class="toc-text">4.2.1 初始化驱动并建立连接</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-2-%E6%96%B0%E5%BB%BAStatement%E8%BF%90%E8%A1%8CSQL%E8%AF%AD%E5%8F%A5"><span class="toc-text">4.2.2 新建Statement运行SQL语句</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-2-3-%E5%85%B3%E9%97%AD%E8%BF%9E%E6%8E%A5"><span class="toc-text">4.2.3 关闭连接</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-%E9%AB%98%E9%98%B6%E6%93%8D%E4%BD%9C"><span class="toc-text">5. 高阶操作</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-1-%E4%BD%BF%E7%94%A8PreparedStatement"><span class="toc-text">5.1 使用PreparedStatement</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-2-%E4%BA%8B%E5%8A%A1"><span class="toc-text">5.2 事务</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-3-ORM%E5%92%8CDAO"><span class="toc-text">5.3 ORM和DAO</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-%E5%8F%82%E8%80%83"><span class="toc-text">6. 参考</span></a></li></ol></div></div></div><div class="card-widget card-recent-post"><div class="card-content"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/post/ceb5/" title="在Deepin上安装Eclipse"><img src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/20210911113215.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="在Deepin上安装Eclipse"/></a><div class="content"><a class="title" href="/post/ceb5/" title="在Deepin上安装Eclipse">在Deepin上安装Eclipse</a><time datetime="2021-09-11T02:46:06.000Z" title="发表于 2021-09-11 10:46:06">2021-09-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/post/6b0/" title="使用VMware创建CentOS7.6虚拟机"><img src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/centos-motif.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="使用VMware创建CentOS7.6虚拟机"/></a><div class="content"><a class="title" href="/post/6b0/" title="使用VMware创建CentOS7.6虚拟机">使用VMware创建CentOS7.6虚拟机</a><time datetime="2021-03-07T14:56:59.000Z" title="发表于 2021-03-07 22:56:59">2021-03-07</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/post/1ea5/" title="前端练习-九宫格"><img src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/353.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="前端练习-九宫格"/></a><div class="content"><a class="title" href="/post/1ea5/" title="前端练习-九宫格">前端练习-九宫格</a><time datetime="2020-09-14T09:06:40.000Z" title="发表于 2020-09-14 17:06:40">2020-09-14</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/post/7240/" title="LeetCode-组合(每日一题)"><img src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/357.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="LeetCode-组合(每日一题)"/></a><div class="content"><a class="title" href="/post/7240/" title="LeetCode-组合(每日一题)">LeetCode-组合(每日一题)</a><time datetime="2020-09-08T07:13:45.000Z" title="发表于 2020-09-08 15:13:45">2020-09-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/post/aeeb/" title="LeetCode-前K个高频元素(每日一题)"><img src="https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/357.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="LeetCode-前K个高频元素(每日一题)"/></a><div class="content"><a class="title" href="/post/aeeb/" title="LeetCode-前K个高频元素(每日一题)">LeetCode-前K个高频元素(每日一题)</a><time datetime="2020-09-07T08:46:04.000Z" title="发表于 2020-09-07 16:46:04">2020-09-07</time></div></div></div></div></div></div></div></main><footer id="footer" style="background-image: url(https://gitee.com/shenshilei1022/hugo_image_resource/raw/master/imgs/132.JPG)"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2021 By Shen Shilei</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">回首向来萧瑟处，归去，也无风雨也无晴。</div><div class="icp"><a><img class="icp-icon" src="/img/icp.png" alt="ICP"/><span>陕ICP备2020013027号</span></a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>function loadValine () {
  function initValine () {
    let initData = {
      el: '#vcomment',
      appId: 'A2SjWuspv5TQF8rzabRx8Y1d-MdYXbMMI',
      appKey: 'SYbn4zrNF0biLCBhwQ0956wm',
      placeholder: '留下你的足迹...(昵称中填写QQ号可使用QQ头像和QQ邮箱)',
      avatar: 'monsterid',
      meta: 'nick,mail,link'.split(','),
      pageSize: '10',
      lang: 'zh-CN',
      recordIP: false,
      serverURLs: '',
      emojiCDN: 'https://cdn.jsdelivr.net/gh/shenshilei1022/emotion@1.0/',
      emojiMaps: {"1":"aru/1.png","2":"aru/2.png","3":"aru/3.png","4":"aru/4.png","5":"aru/5.png","6":"aru/6.png","7":"aru/7.png","8":"aru/8.png","9":"aru/9.png","10":"aru/10.png","11":"aru/11.png","12":"aru/12.png","13":"aru/13.png","14":"aru/14.png","15":"aru/15.png","16":"aru/16.png","17":"aru/17.png","18":"aru/18.png","19":"aru/19.png","20":"aru/20.png","21":"aru/21.png","22":"aru/22.png","23":"aru/23.png","24":"aru/24.png","25":"aru/25.png","26":"aru/26.png","27":"aru/27.png","28":"aru/28.png","29":"aru/29.png","30":"aru/30.png","31":"aru/31.png","32":"aru/32.png","33":"aru/33.png","34":"aru/34.png","35":"aru/35.png","36":"aru/36.png","37":"aru/37.png","38":"aru/38.png","39":"猥琐萌/1.jpg","40":"猥琐萌/2.jpg","41":"猥琐萌/3.jpg","42":"猥琐萌/4.jpg","43":"猥琐萌/5.jpg","44":"猥琐萌/6.jpg","45":"猥琐萌/7.jpg","46":"猥琐萌/8.jpg","47":"猥琐萌/9.jpg","48":"猥琐萌/10.jpg","49":"猥琐萌/11.jpg","50":"猥琐萌/12.jpg","51":"猥琐萌/13.jpg","52":"猥琐萌/14.jpg","53":"猥琐萌/15.jpg","54":"猥琐萌/16.jpg","55":"猥琐萌/17.jpg","56":"猥琐萌/18.jpg","57":"猥琐萌/19.jpg","58":"猥琐萌/20.jpg","59":"猥琐萌/21.jpg","60":"猥琐萌/22.jpg","61":"猥琐萌/23.jpg","62":"猥琐萌/24.jpg","63":"猥琐萌/25.jpg","64":"猥琐萌/26.jpg","65":"猥琐萌/27.jpg","66":"猥琐萌/28.jpg","67":"猥琐萌/29.jpg","68":"猥琐萌/30.jpg","69":"猥琐萌/31.jpg","70":"猥琐萌/32.jpg","71":"猥琐萌/33.jpg","72":"猥琐萌/34.jpg","73":"猥琐萌/35.jpg","74":"猥琐萌/36.jpg","75":"猥琐萌/37.jpg","76":"猥琐萌/38.jpg","77":"猥琐萌/39.jpg","78":"猥琐萌/40.jpg","79":"猥琐萌/41.jpg","80":"猥琐萌/42.jpg","81":"猥琐萌/43.jpg","82":"猥琐萌/44.jpg","83":"猥琐萌/45.jpg","84":"猥琐萌/46.jpg","85":"猥琐萌/47.jpg","86":"猥琐萌/48.jpg","87":"猥琐萌/49.jpg","88":"猥琐萌/50.jpg","89":"猥琐萌/51.jpg","90":"猥琐萌/52.jpg","91":"猥琐萌/53.jpg","92":"猥琐萌/54.jpg","93":"猥琐萌/55.jpg","94":"猥琐萌/56.jpg","95":"猥琐萌/57.jpg","96":"猥琐萌/58.jpg","97":"猥琐萌/59.jpg","98":"猥琐萌/60.jpg","djxbz":"copy/djxbz.jpg","gan":"copy/gan.jpg","hp":"copy/hp.jpg","huaiyi":"copy/huaiyi.jpg","imgcao":"copy/imgcao.jpg","imghua":"copy/imghua.jpg","imgri":"copy/imgri.jpg","mudenggoudai":"copy/mudenggoudai.jpg","pc":"copy/pc.jpg","tlspg":"copy/tlspg.jpg","tql":"copy/tql.jpg","wuyu":"copy/wuyu.jpg","zzxbz":"copy/zzxbz.jpg","icon_huaji":"copy/icon_huaji.png","仙女下凡":"copy/仙女下凡.webp","听歌":"copy/听歌.webp","嫌弃":"copy/嫌弃.webp","害羞":"bilibili/tv_害羞.png","彩虹滑稽":"copy/彩虹滑稽.webp","快走":"copy/快走.webp","扯脸":"copy/扯脸.webp","摸脸":"copy/摸脸.webp","流汗":"bilibili/tv_流汗.png","滑滑板":"copy/滑滑板.webp","点赞":"bilibili/tv_点赞.png","转圈圈":"copy/转圈圈.webp","光头小人1":"copy/光头小人_(1).gif","光头小人19":"copy/光头小人_(19).gif","光头小人20":"copy/光头小人_(20).gif","光头小人21":"copy/光头小人_(21).gif","光头小人22":"copy/光头小人_(22).gif","光头小人23":"copy/光头小人_(23).gif","光头小人24":"copy/光头小人_(24).gif","光头小人25":"copy/光头小人_(25).gif","光头小人26":"copy/光头小人_(26).gif","光头小人27":"copy/光头小人_(27).gif","光头小人28":"copy/光头小人_(28).gif","光头小人29":"copy/光头小人_(29).gif","光头小人30":"copy/光头小人_(30).gif","光头小人31":"copy/光头小人_(31).gif","光头小人32":"copy/光头小人_(32).gif","光头小人33":"copy/光头小人_(33).gif","光头小人34":"copy/光头小人_(34).gif","光头小人35":"copy/光头小人_(35).gif","光头小人36":"copy/光头小人_(36).gif","光头小人37":"copy/光头小人_(37).gif","光波":"copy/光波.gif","厕所1":"copy/厕所_(1).gif","厕所2":"copy/厕所_(2).gif","厕所3":"copy/厕所_(3).gif","咸鱼":"copy/咸鱼.gif","奏乐1":"copy/奏乐_(1).gif","奏乐10":"copy/奏乐_(10).gif","奏乐2":"copy/奏乐_(2).gif","奏乐4":"copy/奏乐_(4).gif","奏乐5":"copy/奏乐_(5).gif","奏乐6":"copy/奏乐_(6).gif","奏乐7":"copy/奏乐_(7).gif","奏乐8":"copy/奏乐_(8).gif","奏乐9":"copy/奏乐_(9).gif","奔跑狗5":"copy/奔跑狗 (5).gif","奔跑狗1":"copy/奔跑狗_(1).gif","奔跑狗2":"copy/奔跑狗_(2).gif","奔跑狗3":"copy/奔跑狗_(3).gif","奔跑狗4":"copy/奔跑狗_(4).gif","害怕滑稽":"copy/害怕滑稽.gif","小人儿10":"copy/小人儿_(10).gif","小兔吃东西12":"copy/小兔吃东西_(12).gif","小兔吃东西13":"copy/小兔吃东西_(13).gif","小兔吃东西14":"copy/小兔吃东西_(14).gif","小兔吃东西18":"copy/小兔吃东西_(18).gif","小兔吃东西19":"copy/小兔吃东西_(19).gif","小兔吃东西20":"copy/小兔吃东西_(20).gif","小兔吃东西21":"copy/小兔吃东西_(21).gif","小兔吃东西22":"copy/小兔吃东西_(22).gif","小疯子2":"copy/小疯子_(2).gif","小疯子3":"copy/小疯子_(3).gif","小疯子4":"copy/小疯子_(4).gif","小疯子5":"copy/小疯子_(5).gif","小疯子6":"copy/小疯子_(6).gif","小疯子7":"copy/小疯子_(7).gif","小疯子8":"copy/小疯子_(8).gif","扇子狗":"copy/扇子狗.gif","抓1":"copy/抓_(1).gif","抓2":"copy/抓_(2).gif","掐脖子":"copy/掐脖子.gif","滑稽1":"copy/滑稽_(1).gif","滑稽2":"copy/滑稽_(2).gif","滑稽3":"copy/滑稽_(3).gif","滑稽4":"copy/滑稽_(4).gif","跪坐1":"copy/跪坐_(1).gif","跪坐2":"copy/跪坐_(2).gif","鹦鹉小恐龙1":"copy/鹦鹉小恐龙_(1).gif","鹦鹉小恐龙2":"copy/鹦鹉小恐龙_(2).gif","doge":"bilibili/tv_doge.png","亲亲":"bilibili/tv_亲亲.png","偷笑":"bilibili/tv_偷笑.png","再见":"bilibili/tv_再见.png","冷漠":"bilibili/tv_冷漠.png","发怒":"bilibili/tv_发怒.png","发财":"bilibili/tv_发财.png","可爱":"bilibili/tv_可爱.png","吐血":"bilibili/tv_吐血.png","呆":"bilibili/tv_呆.png","呕吐":"bilibili/tv_呕吐.png","困":"bilibili/tv_困.png","坏笑":"bilibili/tv_坏笑.png","大佬":"bilibili/tv_大佬.png","大哭":"bilibili/tv_大哭.png","委屈":"bilibili/tv_委屈.png","尴尬":"bilibili/tv_尴尬.png","微笑":"bilibili/tv_微笑.png","思考":"bilibili/tv_思考.png","惊吓":"bilibili/tv_惊吓.png","打脸":"bilibili/tv_打脸.png","抓狂":"bilibili/tv_抓狂.png","抠鼻":"bilibili/tv_抠鼻.png","斜眼笑":"bilibili/tv_斜眼笑.png","无奈":"bilibili/tv_无奈.png","晕":"bilibili/tv_晕.png","流泪":"bilibili/tv_流泪.png","流鼻血":"bilibili/tv_流鼻血.png","生气":"bilibili/tv_生气.png","生病":"bilibili/tv_生病.png","疑问":"bilibili/tv_疑问.png","白眼":"bilibili/tv_白眼.png","皱眉":"bilibili/tv_皱眉.png","目瞪口呆":"bilibili/tv_目瞪口呆.png","睡着":"bilibili/tv_睡着.png","笑哭":"bilibili/tv_笑哭.png","腼腆":"bilibili/tv_腼腆.png","色":"bilibili/tv_色.png","调侃":"bilibili/tv_调侃.png","调皮":"bilibili/tv_调皮.png","鄙视":"bilibili/tv_鄙视.png","闭嘴":"bilibili/tv_闭嘴.png","难过":"bilibili/tv_难过.png","馋":"bilibili/tv_馋.png","鬼脸":"bilibili/tv_鬼脸.png","黑人问号":"bilibili/tv_黑人问号.png","鼓掌":"bilibili/tv_鼓掌.png"},
      enableQQ: true,
      path: window.location.pathname,
    }

    if (true) { 
      initData.requiredFields= ('nick,mail'.split(','))
    }
    
    if (false) {
      const otherData = false
      initData = Object.assign({}, initData, otherData)
    }
    
    const valine = new Valine(initData)
  }

  if (typeof Valine === 'function') initValine() 
  else $.getScript('https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js', initValine)
}

if ('Valine' === 'Valine' || !false) {
  if (false) btf.loadComment(document.querySelector('#vcomment'),loadValine)
  else setTimeout(() => loadValine(), 0)
} else {
  function loadOtherComment () {
    loadValine()
  }
}</script><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><div class="aplayer no-destroy" data-id="4960099490" data-server="netease" data-type="playlist" data-fixed="true" data-mini="true" data-listFolded="false" data-order="random" data-preload="none" data-autoplay="false" muted></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = [
  'title',
  '#config_change',
  '#body-wrap',
  '#rightside-config-hide',
  '#rightside-config-show',
  '.js-pjax'
]

if (false) {
  pjaxSelectors.unshift('meta[property="og:image"]', 'meta[property="og:title"]', 'meta[property="og:url"]')
}

var pjax = new Pjax({
  elements: 'a:not([target="_blank"]):not([href="/post/a7b7/"]):not([href="/artitalk/"]):not([href="/post/6a69/"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  $('script[data-pjax]').each(function () {
    $(this).parent().append($(this).remove())
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  if (typeof gtag === 'function') {
    gtag('config', '', {'page_path': window.location.pathname});
  }

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // Analytics
  if (false) {
    MtaH5.pgv()
  }

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})


document.addEventListener('pjax:send', function () {
  typeof preloader === 'object' && preloader.initLoading()
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  $(window).off('scroll')

  //reset readmode
  $('body').hasClass('read-mode') && $('body').removeClass('read-mode')

})</script></div></body></html>